﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Framework.IOC;
using Framework.IOC.Unity;
using System.Web.Script.Serialization;
using Framework.Common;
using System.Net.Http;
using System.Net;
using Framework.Logger;

namespace Application.WebAPI
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class WebApiApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            //WebApiConfig.Register(GlobalConfiguration.Configuration);

            //Web API,启动特性路由
            GlobalConfiguration.Configure(WebApiConfig.Register);

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            //RouteConfig.RegisterRoutes(RouteTable.Routes);

            IocConfig.RegisterComponents();
            log4net.Config.XmlConfigurator.Configure();
            //LogManager.StartMonitorLogQueue();
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = HttpContext.Current.Server.GetLastError();
            LogManager.getInstance().WriteException("SysLog", ex);
            DataResult result = new DataResult() { Code = ResponseStatusCode.Error, Msg = ex.Message };

            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(result);

            Server.ClearError();
            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Headers.Add("Access-Control-Allow-Origin", "*");
            Response.Write(json);
            
            //HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
            //response.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");

            //Response.Write(response);

        }
        

    }
}